package com.hqhsc.module.manage.activityuser.controller;

import com.hqhsc.module.common.service.CommonService;
import com.hqhsc.module.common.service.FileService;
import com.hqhsc.module.init.service.InitService;
import com.hqhsc.module.login.domain.User;
import com.hqhsc.module.manage.activity.domain.Activity;
import com.hqhsc.module.manage.activityuser.domain.ActivityUser;
import com.hqhsc.module.manage.activityuser.service.ActivityUserManageService;
import com.hqhsc.module.manage.channel.domain.Channel;
import com.hqhsc.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Controller
@RequestMapping("/manage/activityuser")
public class ActivityUserManageController {
	final Logger logger = LoggerFactory.getLogger(ActivityUserManageController.class);

	@Autowired
	private CommonService commonService;
    @Autowired
    private ActivityUserManageService activityUserManageService;
    @Autowired
    private FileService fileService;
	@Autowired
	private Properties appProps;

    private static int IS_NOT_IMPORT = 0;
    private static int IS_IMPORT = 1;

    /**
     * 用户管理-线上报名用户
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/onlinelist" , method = {RequestMethod.GET , RequestMethod.POST})
    public String onlineList(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String pageindexStr = request.getParameter("page_index");//第几页
        int page_index = Integer.parseInt(StringUtils.isBlank(pageindexStr) || "0".equals(pageindexStr) ? "1" : pageindexStr);
        int size = Integer.valueOf(appProps.get("manage.activityuser.online.size").toString());//每页显示条数
        int start = (page_index - 1) * size;

        String user_name = request.getParameter("user_name");

        String method = request.getMethod();
        if("GET".equals(method.toUpperCase()) && StringUtils.isNotEmpty(user_name)) {//get请求，进行编码格式转换
            user_name = this.commonService.characterFormat(user_name , "ISO8859-1" , "UTF-8");
        }

        String activity_id_str = request.getParameter("activity_id");
        int activity_id = -1;
        if(StringUtils.isNotEmpty(activity_id_str)) {
            activity_id = Integer.valueOf(activity_id_str);
        }

        String channel_id_str = request.getParameter("channel_id");
        int channel_id = -1;
        if(StringUtils.isNotEmpty(channel_id_str)) {
            channel_id = Integer.valueOf(channel_id_str);
        }

        String orderby = request.getParameter("orderby");

        Map<String , Object> map = this.activityUserManageService.getActivityUserList(IS_NOT_IMPORT, null , activity_id , channel_id , user_name, orderby , start, size);
        long total = (Long)map.get("total");;
        List<ActivityUser> activity_user_list = (List<ActivityUser>)map.get("activity_user_list");

        List<Channel> channel_list = this.activityUserManageService.getChannelList();
        List<Activity> activity_list = this.activityUserManageService.getActivityList();

        ActivityUser first_view_activity_user = new ActivityUser();
        if(activity_user_list.size() > 0) {
            first_view_activity_user = activity_user_list.get(0);
        }

        long temp = (total - 1) <= 0 ? 0 : (total - 1);
        int pages = Integer.parseInt(Long.toString(temp / size)) + 1;
        int prepages = (page_index - 1) <= 0 ? 1 : (page_index - 1);
        int nextpages = (page_index + 1) >= pages ? pages : (page_index + 1);

        model.addAttribute("current_page" , page_index);
        model.addAttribute("pages" , pages);
        model.addAttribute("prepage" , prepages);
        model.addAttribute("nextpage" , nextpages);
        model.addAttribute("page_url" , request.getRequestURI());

        String condition = "";
        if(StringUtils.isNotEmpty(user_name)) {
            condition = condition + "&username="+user_name;
        }
        if(StringUtils.isNotEmpty(activity_id_str)) {
            condition = condition + "&activity_id="+activity_id_str;
        }
        if(StringUtils.isNotEmpty(channel_id_str)) {
            condition = condition + "&channel_id="+channel_id_str;
        }
        if(StringUtils.isNotEmpty(orderby)) {
            condition = condition + "&orderby="+orderby;
        }
        model.addAttribute("condition" , condition);
        model.addAttribute("user_name" , user_name);
        model.addAttribute("activity_id" , activity_id_str);
        model.addAttribute("channel_id" , channel_id_str);
        model.addAttribute("orderby" , orderby);
        model.addAttribute("activity_user_list" , activity_user_list);
        model.addAttribute("first_view_activity_user" , first_view_activity_user);
        model.addAttribute("channel_list" , channel_list);
        model.addAttribute("activity_list" , activity_list);
        return "/module/manage/activityuser/onlinelist";
    }

    /**
     * 线上报名用户导出，根据活动导出
     * @param model
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/export" , method = RequestMethod.GET)
    public void exportActivityOnlineUsers(Model model , HttpServletRequest request , HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");

        String activity_id_str = request.getParameter("activity_id");
        List<ActivityUser> activity_signup_user_list = this.activityUserManageService.getActivitySignUpUserList(IS_NOT_IMPORT , Long.valueOf(activity_id_str));

        String base_path = request.getSession().getServletContext().getRealPath("/");
        String file_path = appProps.getProperty("activityuser.download.realpath");
        String file_name = "线上报名";
        if(activity_signup_user_list.size() > 0) {
            file_name = activity_signup_user_list.get(0).getActivity_name();
        }

        String path = base_path + file_path + file_name + "-" + DateUtil.getDate(new Date(), "yyyy-MM-dd") +".xls";
        this.fileService.writeActivityOnlineUserExcel(activity_signup_user_list , path);

        this.fileService.exportFile(response , new File(path) , false);
    }

    /**
     * 用户管理-活动到场用户（线下用户）
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importlist" , method = {RequestMethod.GET , RequestMethod.POST})
    public String importList(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String pageindexStr = request.getParameter("page_index");//第几页
        int page_index = Integer.parseInt(StringUtils.isBlank(pageindexStr) || "0".equals(pageindexStr) ? "1" : pageindexStr);
        int size = Integer.valueOf(appProps.get("manage.activityuser.index.size").toString());//每页显示条数
        int start = (page_index - 1) * size;

        String user_name = request.getParameter("user_name");

        String method = request.getMethod();
        if("GET".equals(method.toUpperCase()) && StringUtils.isNotEmpty(user_name)) {//get请求，进行编码格式转换
            user_name = this.commonService.characterFormat(user_name , "ISO8859-1" , "UTF-8");
        }

        String is_buy_str = request.getParameter("is_buy");
        String is_buy = null;
        if(StringUtils.isNotEmpty(is_buy_str)) {
            if("0".equals(is_buy_str)) {
                is_buy = "否";
            } else if("1".equals(is_buy_str)) {
                is_buy = "是";
            }
        }

        String orderby = request.getParameter("orderby");

        Map<String , Object> map = this.activityUserManageService.getActivityUserList(IS_IMPORT, is_buy , -1 , -1 , user_name,orderby, start, size);
        long total = (Long)map.get("total");;
        List<ActivityUser> activity_user_list = (List<ActivityUser>)map.get("activity_user_list");

        long temp = (total - 1) <= 0 ? 0 : (total - 1);
        int pages = Integer.parseInt(Long.toString(temp / size)) + 1;
        int prepages = (page_index - 1) <= 0 ? 1 : (page_index - 1);
        int nextpages = (page_index + 1) >= pages ? pages : (page_index + 1);

        model.addAttribute("current_page" , page_index);
        model.addAttribute("pages" , pages);
        model.addAttribute("prepage" , prepages);
        model.addAttribute("nextpage" , nextpages);
        model.addAttribute("page_url" , request.getRequestURI());

        String condition = "";
        if(StringUtils.isNotEmpty(user_name)) {
            condition = condition + "&user_name="+user_name;
        }
        if(StringUtils.isNotEmpty(is_buy_str)) {
            condition = condition + "&is_buy="+is_buy_str;
        }
        if(StringUtils.isNotEmpty(orderby)) {
            condition = condition + "&orderby="+orderby;
        }
        model.addAttribute("condition" , condition);
        model.addAttribute("is_buy" , is_buy_str);
        model.addAttribute("orderby" , orderby);
        model.addAttribute("user_name" , user_name);
        model.addAttribute("activity_user_list" , activity_user_list);
        return "/module/manage/activityuser/importlist";
    }

    /**
     * 用户管理-导入用户
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/import" , method = RequestMethod.GET)
    public String importActivityUser(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        return "/module/manage/activityuser/import";
    }

    /**
     * 用户管理-处理excel导入用户
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/doimport" , method = RequestMethod.POST)
//    @ResponseBody
    public String doImportActivityUser(HttpServletRequest request, HttpServletResponse response , @RequestParam("file_upload") CommonsMultipartFile file_upload) {
        User user = (User)request.getSession().getAttribute("user");
        this.activityUserManageService.doImportActivityUser(request , file_upload , user);
        return "redirect:/manage/activityuser/importlist";
    }














    //************************jiangyin 以下二期才会涉及*****************************************************

//    /**
//     * 用户管理-待支付用户管理
//     * @param model
//     * @param request
//     * @param response
//     * @return
//     */
//	@RequestMapping(value = "/forpaylist" , method = {RequestMethod.GET , RequestMethod.POST})
//	public String forPayList(Model model , HttpServletRequest request , HttpServletResponse response) {
//		User user = (User)request.getSession().getAttribute("user");
//
//        String pageindexStr = request.getParameter("page_index");//第几页
//        int page_index = Integer.parseInt(StringUtils.isBlank(pageindexStr) || "0".equals(pageindexStr) ? "1" : pageindexStr);
//        int size = Integer.valueOf(appProps.get("manage.activityuser.index.size").toString());//每页显示条数
//        int start = (page_index - 1) * size;
//
//        String user_name = request.getParameter("username");
//
//        String method = request.getMethod();
//        if("GET".equals(method.toUpperCase())) {//get请求，进行编码格式转换
//            user_name = this.commonService.characterFormat(user_name , "ISO8859-1" , "UTF-8");
//        }
//
//        Map<String , Object> map = this.activityUserManageService.getActivityUserList(0, user_name, start, size);
//        long total = (Long)map.get("total");;
//        List<ActivityUser> activity_user_list = (List<ActivityUser>)map.get("activity_user_list");
//
//        long temp = (total - 1) <= 0 ? 0 : (total - 1);
//        int pages = Integer.parseInt(Long.toString(temp / size)) + 1;
//        int prepages = (page_index - 1) <= 0 ? 1 : (page_index - 1);
//        int nextpages = (page_index + 1) >= pages ? pages : (page_index + 1);
//
//        model.addAttribute("current_page" , page_index);
//        model.addAttribute("pages" , pages);
//        model.addAttribute("prepage" , prepages);
//        model.addAttribute("nextpage" , nextpages);
//        model.addAttribute("page_url" , request.getRequestURI());
//
//        String condition = "";
//        if(StringUtils.isNotEmpty(user_name)) {
//            condition = condition + "&username="+user_name;
//        }
//        model.addAttribute("condition" , condition);
//
//        model.addAttribute("user_name" , user_name);
//        model.addAttribute("activity_user_list" , activity_user_list);
//        return "/module/manage/activityuser/forpaylist";
//	}
//
//    /**
//     * 用户管理-交易管理
//     * @param model
//     * @param request
//     * @param response
//     * @return
//     */
//    @RequestMapping(value = "/deallist" , method = {RequestMethod.GET , RequestMethod.POST})
//    public String dealList(Model model , HttpServletRequest request , HttpServletResponse response) {
//        User user = (User)request.getSession().getAttribute("user");
//
//        String pageindexStr = request.getParameter("page_index");//第几页
//        int page_index = Integer.parseInt(StringUtils.isBlank(pageindexStr) || "0".equals(pageindexStr) ? "1" : pageindexStr);
//        int size = Integer.valueOf(appProps.get("manage.activityuser.index.size").toString());//每页显示条数
//        int start = (page_index - 1) * size;
//
//        String user_name = request.getParameter("username");
//
//        String method = request.getMethod();
//        if("GET".equals(method.toUpperCase())) {//get请求，进行编码格式转换
//            user_name = this.commonService.characterFormat(user_name , "ISO8859-1" , "UTF-8");
//        }
//
//        Map<String , Object> map = this.activityUserManageService.getActivityUserList(0, user_name, start, size);
//        long total = (Long)map.get("total");;
//        List<ActivityUser> activity_user_list = (List<ActivityUser>)map.get("activity_user_list");
//
//        long temp = (total - 1) <= 0 ? 0 : (total - 1);
//        int pages = Integer.parseInt(Long.toString(temp / size)) + 1;
//        int prepages = (page_index - 1) <= 0 ? 1 : (page_index - 1);
//        int nextpages = (page_index + 1) >= pages ? pages : (page_index + 1);
//
//        model.addAttribute("current_page" , page_index);
//        model.addAttribute("pages" , pages);
//        model.addAttribute("prepage" , prepages);
//        model.addAttribute("nextpage" , nextpages);
//        model.addAttribute("page_url" , request.getRequestURI());
//
//        String condition = "";
//        if(StringUtils.isNotEmpty(user_name)) {
//            condition = condition + "&username="+user_name;
//        }
//        model.addAttribute("condition" , condition);
//
//        model.addAttribute("user_name" , user_name);
//        model.addAttribute("activity_user_list" , activity_user_list);
//        return "/module/manage/activityuser/deallist";
//    }
//
//    /**
//     * 用户管理-交易管理
//     * @param model
//     * @param request
//     * @param response
//     * @return
//     */
//    @RequestMapping(value = "/paiedlist" , method = {RequestMethod.GET , RequestMethod.POST})
//    public String paiedList(Model model , HttpServletRequest request , HttpServletResponse response) {
//        User user = (User)request.getSession().getAttribute("user");
//
//        String pageindexStr = request.getParameter("page_index");//第几页
//        int page_index = Integer.parseInt(StringUtils.isBlank(pageindexStr) || "0".equals(pageindexStr) ? "1" : pageindexStr);
//        int size = Integer.valueOf(appProps.get("manage.activityuser.index.size").toString());//每页显示条数
//        int start = (page_index - 1) * size;
//
//        String user_name = request.getParameter("username");
//
//        String method = request.getMethod();
//        if("GET".equals(method.toUpperCase())) {//get请求，进行编码格式转换
//            user_name = this.commonService.characterFormat(user_name , "ISO8859-1" , "UTF-8");
//        }
//
//        Map<String , Object> map = this.activityUserManageService.getActivityUserList(0, user_name, start, size);
//        long total = (Long)map.get("total");;
//        List<ActivityUser> activity_user_list = (List<ActivityUser>)map.get("activity_user_list");
//
//        long temp = (total - 1) <= 0 ? 0 : (total - 1);
//        int pages = Integer.parseInt(Long.toString(temp / size)) + 1;
//        int prepages = (page_index - 1) <= 0 ? 1 : (page_index - 1);
//        int nextpages = (page_index + 1) >= pages ? pages : (page_index + 1);
//
//        model.addAttribute("current_page" , page_index);
//        model.addAttribute("pages" , pages);
//        model.addAttribute("prepage" , prepages);
//        model.addAttribute("nextpage" , nextpages);
//        model.addAttribute("page_url" , request.getRequestURI());
//
//        String condition = "";
//        if(StringUtils.isNotEmpty(user_name)) {
//            condition = condition + "&username="+user_name;
//        }
//        model.addAttribute("condition" , condition);
//
//        model.addAttribute("user_name" , user_name);
//        model.addAttribute("activity_user_list" , activity_user_list);
//        return "/module/manage/activityuser/paiedlist";
//    }
}